package br.com.hstar.client.quarto;

import java.util.List;

import br.com.hstar.client.ServiceFactory;
import br.com.hstar.client.tabelapreco.MyPrecoImage;
import br.com.hstar.client.tabelapreco.PrecoUI;

import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.ClickListener;
import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.DockPanel;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.SimplePanel;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.Widget;

public class PrecoQuartosDialogBox extends DialogBox {
	
	private DockPanel 	dockPanel;
	private SimplePanel contentPanel;
	private FlexTable 	table;
	
	//textbox que vai receber o aumento de preço
	private TextBox tx;
	
	public PrecoQuartosDialogBox(TextBox textbox) {
		tx = textbox;
		init();
		this.setStyleName("gwt-DialogBox2");
	}
	
	public void init() {
		dockPanel = new DockPanel();
	    dockPanel.setSpacing(10);
	    preencheDockPanel(dockPanel);
	    	    
	    //tabela com o conteúdo
	    table = new FlexTable();
	    preencheTabela();
		contentPanel.setWidget(table);
	    
	    this.setWidget(dockPanel);
	}
	
	public void preencheDockPanel(DockPanel dp) {
		HTML html = new HTML( "Tabela de Preços" );
		html.setStyleName( "IntPanel-Title" );
		dp.add( html, DockPanel.NORTH );
		
		contentPanel = new SimplePanel();
		//contentPanel.setStyleName( "ContentTable" );
		dp.add( contentPanel, DockPanel.CENTER );
	}
	
	public void preencheTabela() {
		ServiceFactory.createPrecoService().listAll(new ListAllPrecosCallBack());
	}
	
	private class ListAllPrecosCallBack implements AsyncCallback {
		public void onFailure(Throwable caught) {
			Window.alert("ERRO ao listar os preços");
		}
		public void onSuccess(Object result) {
			List precos = (List) result;
			for (int i=0; i < precos.size(); i++) {
				PrecoUI pUI = (PrecoUI) precos.get(i);
				
				final MyPrecoImage img = new MyPrecoImage( getImageUrl("img/as.png") );
				img.setPreco(pUI);
				
				int c = table.getRowCount();
				table.setWidget(c, 0, img);
				table.setWidget(c, 1, new HTML(pUI.getDescricao()));
				table.setWidget(c, 2, new HTML(pUI.getHoraEntrada()));
				table.setWidget(c, 3, new HTML((pUI.getHoraSaida().trim().equals("")? " " : " às ")));
				table.setWidget(c, 4, new HTML(pUI.getHoraSaida()));
				table.setWidget(c, 5, new HTML("R$ "+pUI.getValor()));
				
				img.addClickListener(new ClickListener(){
					public void onClick(Widget arg0) {
						if(Window.confirm("Deseja confirmar o preço selecionado?")) {
							String text = tx.getText();
							try{
								String preco = img.getPreco().getValor().replace(',', '.');
								double precoQuarto = Double.parseDouble(preco);
								
								double precoAtual = 0;
								if (!text.trim().equals(""))
									precoAtual = new Double(text).doubleValue();
								
								tx.setText((precoAtual+precoQuarto)+"");
								
							} catch (NumberFormatException nf){
							}
						}
						hide();
					}
				});							
			}
		}
	}
	
	public static String getImageUrl( String relImgUrl ) {
		return GWT.getModuleBaseURL() + relImgUrl;
	}
	
	//mexer na posicao
	public void setPopupPosition(int arg0, int arg1) {
		super.setPopupPosition(500, 400);
	}
	
	public native int getBodyScrollLeft() /*-{
	  var scrOfX = 0;
	  if( typeof( $wnd.pageYOffset ) == 'number' ) {
	    //Netscape compliant
	    scrOfX = $wnd.pageXOffset;
	  } else if( $doc.body && ( $doc.body.scrollLeft || $doc.body.scrollTop ) ) {
	    //DOM compliant
	    scrOfX = $doc.body.scrollLeft;
	  } else if( $doc.documentElement && ( $doc.documentElement.scrollLeft || $doc.documentElement.scrollTop ) ) {
	    //IE6 standards compliant mode
	    scrOfX = $doc.documentElement.scrollLeft;
	  }
	  return scrOfX;
	}-*/;
	
	private native int getBodyScrollTop() /*-{
	  var scrOfY = 0;
	  if( typeof( $wnd.pageYOffset ) == 'number' ) {
	    //Netscape compliant
	    scrOfY = $wnd.pageYOffset;
	  } else if( $doc.body && ( $doc.body.scrollLeft || $doc.body.scrollTop ) ) {
	    //DOM compliant
	    scrOfY = $doc.body.scrollTop;
	  } else if( $doc.documentElement && ( $doc.documentElement.scrollLeft || $doc.documentElement.scrollTop ) ) {
	    //IE6 standards compliant mode
	    scrOfY = $doc.documentElement.scrollTop;
	  }
	  return scrOfY;
	}-*/;
}
